print("Code Chunk")[1] "Code Chunk"
January 9, 2025
We are expected to have installed R and RStudio, if not see the installing R section.
In the discussion section, we will focus on coding and practicing what we have learned in the lectures.
Office hours are on Tuesday, 11-12:30 Scott 110.
Questions?
To insert a Code Chunk, you can use Ctrl+Alt+I on Windows and Cmd+Option+I on Mac. Run the whole chunk by clicking the green triangle, or one/multiple lines by using Ctrl + Enter or Command + Return on Mac.
Most of the functions we want to run require an argument For example, the function print() above takes the argument “Code Chunk”.
There are many data structures, but the most important to know the following.
c().$ operator.We work with various classes of data, and the analysis we perform depends heavily on these classes.
As you noticed, R did not identify the class of data correctly. We can change it using as.factor() function. You can easily change the class of your variable (as.numeric(), as.integer(), as.character())
Quite frequently, we will use additional libraries to extend the capabilities of R. I’m sure you remember tidyverse. Let’s load it.
If you updated your R or recently downloaded it, you can easily install libraries using the function install.packages().
Pipes (%>% or |>) are helpful for streamlining the coding. They introduce linearity to the process of writing the code. In plain English, a pipe translates to “take an object, and then”.
Base R vs Tidyverse
Useful functions, sample()
Visualizations
Tidyverse basics (mutate, filter, select, summarize, etc) Descriptive statistics Confidence intervals
| Statistic | Function | Example Usage |
|---|---|---|
| Minimum | min() |
min(x) |
| Maximum | max() |
max(x) |
| Mean | mean() |
mean(x) |
| Median | median() |
median(x) |
| Standard Deviation | sd() |
sd(x) |
| Variance | var() |
var(x) |
| Sum | sum() |
sum(x) |
| Summary | summary() |
summary(x) |
First, we need to install R. Click the button below and click “Download and Install R”. Choose your OS. For Windows you need to download “base”; for MacOS and Linux you have to choose the version of your OS. Install.
For windows:
Second, we need to install RStudio. Click the button below and click “Download RStudio Desktop”. You will be redirected to your version automatically. Install.